package com.jzhao.jpre.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.jzhao.jpre.base.StringPublicDeal;
import com.jzhao.jpre.model.Device;

public class DeviceDao {
	protected JdbcTemplate jdbcTemplate;
	protected StringPublicDeal strPubDeal;

	class DeviceRowMapper implements RowMapper{
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Device device = new Device();
			device.setDevNo(strPubDeal.trim(rs.getString("DevNo")));
			device.setDistrictNo(strPubDeal.trim(rs.getString("DistrictNo")));
			device.setDevStat(strPubDeal.trim(rs.getString("DevStat")));
			device.setLogLvl(strPubDeal.trim(rs.getString("LogLvl")));
			device.setDevIp(strPubDeal.trim(rs.getString("DevIp")));
			device.setChkFlag(strPubDeal.trim(rs.getString("ChkFlag")));
	
			return device;
		}
	}
	
	public void setDataSource(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public void setStrPubDeal(StringPublicDeal strPubDeal) {
		this.strPubDeal = strPubDeal;
	}
	
	public Device selectByDevNoAndIP(String devId,String ip) throws DataAccessException {
		Device device=null;
		String sql="SELECT * FROM t_jb_device WHERE devNo='" + devId.trim() + "' AND devIp='" + ip.trim() + "'";
		device =(Device)jdbcTemplate.queryForObject(sql, new DeviceRowMapper());
		return device;
	}
	
	public Device selectByDevNo(String devId) throws DataAccessException {
		Device device=null;
		String sql="SELECT * FROM t_jb_device WHERE devNo='" + devId.trim() + "'";
		device =(Device)jdbcTemplate.queryForObject(sql, new DeviceRowMapper());
		return device;
	}
}
